/**
 * Created by wangjian on 17/8/28.
 */
myApp.controller('AuthorizeCController', function ($scope, $uibModal, $sce, ngDialog, $http, $interval) {
    $scope.info = {};
    $scope.info.c_ucloud_list = c_ucloud_list;
    $scope.info.selectC = $scope.info.c_ucloud_list[0];
    $scope.info.authorize = {
        'auth_qa': false,
        'auth_ops': false,
        'ogg_status': false,
        'completeDate': '',
        'tom_flag': false
    };
    $scope.info.errMsg = null;
    $scope.info.oggRunStatus = null;
    $scope.info.oggRunTotal = null;
    $scope.info.pmsChangeLog = [];
    $scope.info.checkData = null;

    $scope.timer = null;
    $scope.pmsTimer = null;

    $scope.cleanTimer = function () {
        if ($scope.timer != null) {
            $interval.cancel($scope.timer);
            $scope.timer = null;
        }
        if ($scope.pmsTimer != null) {
            $interval.cancel($scope.pmsTimer);
            $scope.pmsTimer = null;
        }
    };

    $scope.$on("$destroy", function () {
        $scope.cleanTimer();
    });

    //获取pms修改记录
    $scope.getPmsChangeLog = function () {
        var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/changelog';
        $http.post(java_url + '/beetle/uCloud/Cget', {
            tokenId: getTokenId(),
            url: url
        }).success(function (data) {
            if (data.code == '0') {
                $scope.info.pmsChangeLog = data.list;
            }
        }).error(function () {
            $scope.info.errMsg = '请求ogg-web-client异常';
        });
    };
    $scope.startPmsTimer = function () {
        if ($scope.pmsTimer == null) {
            $scope.pmsTimer = $interval(function () {
                $scope.getPmsChangeLog();
            }, 60000);
        }
    };

    //切换机器
    $scope.changeC = function () {
        var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/status';
        $scope.info.errMsg = null;
        $scope.cleanTimer();
        $scope.info.oggRunStatus = null;
        $scope.info.oggRunTotal = null;
        $scope.info.errMsg = null;
        $scope.info.pmsChangeLog = [];
        doBeetlePost($http, $uibModal, java_url + '/beetle/uCloud/Cget', {
            url: url,
            tokenId: getTokenId()
        }, function (data) {
            $scope.info.authorize = data;
            $scope.getOgg();
        }, function () {
            $scope.info.errMsg = '请求ogg-web-client异常';
        });
        $scope.startPmsTimer();
    };
    $scope.changeC();

    //执行操作
    $scope.authorize = function (type) {
        var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/do';

        var msg = "";
        if (type == 'auth_qa' || type == 'auth_ops') {
            msg = '确定要进行授权吗?';
        } else if (type == 'cancel_qa' || type == 'cancel_ops') {
            msg = '确定要取消授权吗?';
        } else if (type == "cancel_all") {
            msg = '确定要禁用所有授权吗?';
        } else if (type == 'start_ogg' || type == 'start_white') {
            msg = '确定要启动ogg服务吗?';
        } else if (type == 'stop_ogg') {
            msg = '确定要停止ogg服务吗?';
        }

        openDialog(ngDialog, $scope, msg, true, function (val) {
            if (!val) {
                return;
            }
            doBeetlePost($http, $uibModal, java_url + '/beetle/uCloud/Cget', {
                tokenId: getTokenId(),
                url: url,
                type: type
            }, function (data) {
                if (data.code == '0') {
                    if (data.code == '0') {
                        $scope.changeC();
                    } else {
                        if (typeof data.desc != undefined && data.desc != undefined) {
                            alert(JSON.stringify(data.desc));
                        } else {
                            alert(JSON.stringify(data.message));
                        }
                    }
                } else {
                    if (typeof data.desc != undefined && data.desc != undefined) {
                        alert(JSON.stringify(data.desc));
                    } else {
                        alert(JSON.stringify(data.message));
                    }
                }
            }, function () {
                $scope.info.errMsg = '请求ogg-web-client异常';
            });
        });
    };

    //查询ogg服务运行状态
    $scope.getOggRunStatus = function () {
        var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/ogg';

        $http.post(java_url + '/beetle/uCloud/Cget', {
            tokenId: getTokenId(),
            url: url
        }).success(function (data) {
            $scope.info.errMsg = null;
            if (data.code == '0') {
                $scope.info.oggRunStatus = data.message;
                // $scope.getOggRunTotal();
            } else {
                $scope.info.oggRunStatus = null;
                $scope.info.errMsg = JSON.stringify(data.message);
            }
        }).error(function () {
            $scope.info.oggRunStatus = null;
            $scope.info.errMsg = '请求ogg-web-client异常';
        });
    };

    //查询ogg同步数据量
    // $scope.getOggRunTotal = function () {
    //     if ($scope.info.oggRunStatus != 'RUNNING') {
    //         $scope.info.oggRunTotal = null;
    //         return;
    //     }
    //
    //     var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/getLog';
    //     $http.post(java_url + '/beetle/uCloud/Cget', {
    //         tokenId: getTokenId(),
    //         url: url
    //         // ip: $scope.info.selectC
    //     }).success(function (data) {
    //         $scope.info.errMsg = null;
    //         if (data.code == '0') {
    //             $scope.info.oggRunTotal = data.data;
    //         } else {
    //             $scope.info.oggRunTotal = null;
    //             $scope.info.errMsg = JSON.stringify(data.message);
    //         }
    //     }).error(function () {
    //         $scope.info.oggRunTotal = null;
    //         $scope.info.errMsg = '请求ogg-web-client异常';
    //     });
    // };

    //检查同步数据
    $scope.checkData = function (type) {
        var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/check';
        openDialog(ngDialog, $scope, "确定要检查同步数据吗？", true, function (val) {
            if (!val) {
                return;
            }
            $http.post(java_url + '/beetle/uCloud/Cget', {
                tokenId: getTokenId(),
                url: url,
                type: type
            }).success(function (data) {
                $scope.info.errMsg = null;
                if (data.code == '0') {
                    $scope.info.checkData = $sce.trustAsHtml(data.message.replace(/,/g, "<br>"));
                } else {
                    $scope.info.checkData = null;
                    $scope.info.errMsg = JSON.stringify(data.message);
                }
            }).error(function () {
                $scope.info.checkData = null;
                $scope.info.errMsg = '请求ogg-web-client异常';
            });
        });
    };

    //ogg设置为启动时  查询ogg信息
    $scope.getOgg = function () {
        $scope.getOggRunStatus();
        if ($scope.timer == null) {
            $scope.timer = $interval(function () {
                $scope.getOggRunStatus();
                var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/status';
                $http.post(java_url + '/beetle/uCloud/Cget', {
                    tokenId: getTokenId(),
                    url: url
                }).success(function (data) {
                    $scope.info.authorize = data;
                }).error(function () {
                    $scope.info.errMsg = '请求ogg-web-client异常';
                });
            }, 30000);
        }
    };

    //清空redis缓存
    $scope.cleanRedis = function () {
        openDialog(ngDialog, $scope, '确定要清空redis缓存么？', true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, '再想一下要清空缓存么？', true, function (val) {
                if (!val) {
                    return;
                }
                doBeetlePost($http, $uibModal, java_url + '/beetle/uCloud/ogg/clean', {
                    tokenId: getTokenId(),
                    ip: $scope.info.selectC
                }, function (data) {
                    var desc = null;
                    if (data.code == '0') {
                        for (i in data.data) {
                            if (data.data[i].status != 'OK') {
                                desc = desc + data.data[i].ip + ":" + data.data[i].status + "\r\n";
                            }
                        }
                        if (desc != null) {
                            alert("清空缓存失败,请联系管理员!\r\n" + desc);
                        } else {
                            alert("清空缓存成功!");
                        }
                    } else {
                        alert(JSON.stringify(data.message));
                    }
                }, function () {
                    $scope.info.errMsg = '请求ogg-web-client异常';
                });
            });
        });
    };
    //清空本机redis缓存
    $scope.cleanLocalRedis = function () {
        openDialog(ngDialog, $scope, '确定要清空本机redis缓存么？', true, function (val) {
            if (!val) {
                return;
            }
            openDialog(ngDialog, $scope, '再想一下要清空本机缓存么？', true, function (val) {
                if (!val) {
                    return;
                }
                var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/cleanRedis';
                $http.post(java_url + '/beetle/uCloud/Cget', {
                    tokenId: getTokenId(),
                    url: url
                }).success(function (data) {
                    if (data.code == '0') {
                        alert("清空缓存成功!");
                    } else {
                        $scope.info.errMsg = data.message;
                    }
                }).error(function () {
                    $scope.info.errMsg = '请求ogg-web-client异常';
                });
            });
        });
    };

    $scope.download = function (fileName) {
        openDialog(ngDialog, $scope, '下载ogg同步信息文件 ？', true, function (val) {
            if (!val) {
                return;
            }
            window.location.href = java_url + "/beetle/uCloud/ogg/download?ip=" + $scope.info.selectC
                + "&tokenId=" + getTokenId() + '&fileName=' + fileName + ".log";
        });
    };

    $scope.changeTomStatus = function (flag) {
        openDialog(ngDialog, $scope, '确定要修改TOM配置信息,开启配置么？', true, function (val) {
            if (!val) {
                return;
            }
            var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/updateTom';
            $http.post(java_url + '/beetle/uCloud/Cget', {
                tokenId: getTokenId(),
                url: url,
                flag: flag
            }).success(function (data) {
                $scope.changeC();
            }).error(function () {
                $scope.info.errMsg = '请求ogg-web-client异常';
            });
        });
    };
    //预算配置同步
    $scope.budget = function (flag) {
        openDialog(ngDialog, $scope, '确定要同步预算配置么？', true, function (val) {
            if (!val) {
                return;
            }
            var url = 'http://' + $scope.info.selectC + ':' + c_port + '/api/v1/ogg/budget';
            $http.post(java_url + '/beetle/uCloud/Cget', {
                tokenId: getTokenId(),
                url: url
            }).success(function (data) {
                openDialogAlert(ngDialog, $scope, "同步成功！");
            }).error(function () {
                $scope.info.errMsg = '请求ogg-web-client异常';
            });
        });
    };
});